import { _decorator, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { UIBase } from '../../FrameWork3.8.6/FrameWork/UIFramework/UIBase';
import { PropertyDt } from '../../FrameWork3.8.6/FrameWork/Common/CommonInterface';
import { ResMgr } from '../../FrameWork3.8.6/FrameWork/Res/ResMgr';
import { BundleConfig } from '../../FrameWork3.8.6/FrameWork/NeedToChange/BundleConfig';
import { Emitter } from '../../FrameWork3.8.6/FrameWork/Common/Emitter';
import { UIManager } from '../../FrameWork3.8.6/FrameWork/UIFramework/UIManager';
import { BagManager } from '../Game/Manager/BagManager';
import { PlayerPropertyBase } from './PlayerPropertyBase';
const { ccclass, property } = _decorator;


@ccclass('DetailsPanel')
export class DetailsPanel extends UIBase {
    //打开当前UI的UI名字
    private _arrUIName:string[]=[];
    onInit(...rest: any[]): void {
        //我这里手动写上
        this._arrUIName=["EquipmentPanel","SkillPanel","ShiBingPanel"];
        this.addClickEventListener("_CloseBtn",()=>{
            this.closeSelf(true);
        })
    }

    //剩余参数的第一个是数据基类，第二个是拾取的数量
    onEnter(...rest: any[]): void {
        //设置层级在最上面
        let propertyDt=rest[0] as PropertyDt
        this.node.setSiblingIndex(100000);
        this.refreshData(propertyDt,rest[1]);

        //添加装备事件
        this.addClickEventListener("_EquipBtn",()=>{
            //通知背包数据更新当前的已装备数据
            BagManager.instance.addBagDt(propertyDt.type,propertyDt,true);
            //直接通知页面再更新一次背包数据
            for(let uiName of this._arrUIName){
                let ui=UIManager.instance.getUI(uiName) as PlayerPropertyBase;
                if(ui){
                    Emitter.instance.emit("refreshEquipmentDt",ui);
                    this.closeSelf(true);
                    return;
                }
            }
            this.closeSelf();
        })
    }



    //更新数据的方法
    refreshData(propertyDt:PropertyDt,ownNum:number){
        this.getComp<Label>("_AckLabel","Label").string=propertyDt.ack.toString();
        this.getComp<Label>("_DiscriptionLabel","Label").string=propertyDt.description;
        this.getComp<Sprite>("_MaterialImg","Sprite").spriteFrame=ResMgr.instance.getRes<SpriteFrame>(BundleConfig.Img.name,propertyDt.iconName);
        this.getComp<Label>("_OwnNum","Label").string=ownNum.toString();
        this.getComp<Label>("_TotalNum","Label").string="/"+propertyDt.requirement;
        

    }
}


